package dog_mall_mycenter.controller;

import dog_mall_commons.pojo.Dogs;
import dog_mall_commons.pojo.Users;
import dog_mall_login.service.DoLogin;
import dog_mall_mycenter.service.MycenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class MyCenterController {
    @Autowired
    private MycenterService test;

    @Autowired
    private DoLogin doLogin;

    @RequestMapping("/mycenter1")
    public String sss(Model model){
        List<Users> usersList = test.findall();
        model.addAttribute("usersList",usersList);
        return "mycenter";
    }

    @RequestMapping("/sellDog")
    public String sellDog(){
        return "sellDog";
    }
    @RequestMapping("/mycenter")
    public String mycenter(){
        return "mycenter";
    }
    @RequestMapping("/shouchang")
    public String shouchang(){
        return "shouchang";
    }
    @RequestMapping("/gouwuche")
    public String gouwuche(){
        return "gouwuche";
    }
    @RequestMapping("/dizhi")
    public String dizhi(){
        return "dizhi";
    }
    @RequestMapping("/uppwd")
    public String uppwd(){
        return "uppwd";
    }
    @RequestMapping("/about")
    public String about(){
        return "about";
    }
    @RequestMapping("/updatePwd")
    public String updatePwd(String id, String pwd, String oldPwd,String newPwd1,String newPwd2, HttpServletRequest request){
        String info ="";
        System.out.println(id +"pwd="+pwd+"oldpwd="+oldPwd);

        if(oldPwd.equals("")||newPwd1.equals("")||newPwd2.equals("")){
            info="密码不能为空";
            request.setAttribute("info1",info);
            return "uppwd";
        }

        else if(!pwd.equals(oldPwd)){
            info="旧密码不对";
            request.setAttribute("info1",info);
            return "uppwd";
        }

        else if (!newPwd1.equals(newPwd2)){
            info="两次密码不对应";
            request.setAttribute("info1",info);
            return "uppwd";
        }
        else {
            Integer ids =Integer.parseInt(id);
            test.UpdatePwdById(ids,newPwd1);
            info="密码修改成功";
            request.setAttribute("info1",info);
        }

        return "uppwd";
    }

    /*新增地址的跳转方法*/
    @RequestMapping(value ="/insertaddr")
    public String insertaddr(Integer id ,HttpServletRequest request){
        request.setAttribute("userid",id);
        return "addaddr";
    }
    /*新增地址方法*/
    @RequestMapping("useraddr")
    public String useraddr(HttpServletRequest request,Integer id, String street,String quyu,String number){
        System.out.println("区域="+quyu+"街道="+street+"门牌号"+number);
        String add ="";
        String str ="";
        String num ="";
        if(quyu.equals("0")){ add="锦江区"; }
        if(quyu.equals("1")){ add="武侯区"; }
        if(quyu.equals("2")){ add="高新区"; }
        if(street.equals("0")){ str="步行街"; }
        if(street.equals("1")){ str="商业街"; }
        if(street.equals("2")){ str="小西街";}
        if(number.equals("0")){num="000~100号";}
        if(number.equals("1")){num="100~200号";}
        if(number.equals("2")){num="200~300号";}
        String address=add+str+num;
        test.updateAddrById(id,address);
        Users users = test.selectuserById(id);
        request.getSession().setAttribute("user",users);
        return "dizhi";
    }
    /*删除地址方法*/
    @RequestMapping("/deleteaddr")
    public String deleteaddr(Integer id,HttpServletRequest request){
        test.deleteAddrById(id);
        Users users = test.selectuserById(id);
        request.getSession().setAttribute("user",users);
        return "dizhi";
    }


    @RequestMapping(value = "/updateusername")
    public String updateusername(String username,String id,HttpServletRequest request){
        System.out.println(id);
        System.out.println(username);
        if(!username.equals("")&&id!=null){
            Integer ids = Integer.parseInt(id);
            test.UpdateUsernameById(ids,username);
           Users users = test.selectuserById(ids);
            request.getSession().setAttribute("user",users);
        }

        return "mycenter";
    }

    /*删除发布信息*/
    @RequestMapping("deletedogs")
    public String deletedogs(Integer id,Integer userid,HttpServletRequest request ){
        test.deleteDogById(id);
        //更新发布信息
        List<Dogs> dogs1 = doLogin.findAllDogs(userid);
        request.getSession().setAttribute("dog_img",dogs1);

        return "mycenter";
    }
    /*修改发布信息*/
    @RequestMapping("/update_dog_info")
    public  String update_dog_info(){
        return "update_dog_info";
    }


    @RequestMapping("/sellDogInfo")
    public String sellDogInfo(String userId, String leixing,String pingzhong,String price,String age,String info,String sex,HttpServletRequest request){
        if(leixing.equals("")&&pingzhong.equals("")&&price.equals("")&&age.equals("")&&info.equals("")&&age.equals("")){
            request.setAttribute("infos","提交失败,输入不完整");
            return "sellDog";
        }
        System.out.println(userId+leixing+pingzhong+price+age+info+sex);
        Integer userid = Integer.parseInt(userId);
        Integer ages = Integer.parseInt(age);
        Dogs dogs = test.selectMaxIdForUser(userid);
        // 时间转换
       /* Date currentTime = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
       *//* String formatData = format.format(currentTime);*//*
        Date dd = format.parse(currentTime)*/
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(dateString, pos);
        dogs.setDogReleaseDate(currentTime_2);
        dogs.setDogType(leixing);
        dogs.setDogName(pingzhong);
        dogs.setDogPrices(price);
        dogs.setDogContent(info);
        dogs.setDogSex(sex);
        dogs.setDogAge(ages);
        test.updateDogs(dogs);
        List<Dogs> dogs1 = doLogin.findAllDogs(userid);
        request.getSession().setAttribute("dog_img",dogs1);

        return "mycenter";
    }


    @RequestMapping(value = "/upload")
    public void  onUpload(MultipartFile file, HttpServletRequest request,String id) throws IOException {
        // 定义上传的路径
        String path = request.getSession().getServletContext().getRealPath("/dog_img/");
        File file1 = new File(path);
        System.out.println(path);
        //判断目录是否存在
        if (!file1.exists()){
            file1.mkdirs();
        }
        if (file!=null){
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            System.out.println( "名字"+originalFilename);
            // uuid作为文件名
            String uuid = UUID.randomUUID().toString().replaceAll("-","");

            String contentype= file.getContentType();
            String name = contentype.substring(contentype.indexOf("/")+1);

            originalFilename =uuid+"."+name;
            // 自定义文件名
            request.setAttribute("name",originalFilename);
            //上传
            file.transferTo(new File(path+originalFilename));
            Integer ids = Integer.parseInt(id);
            Dogs dogs = new Dogs();
            dogs.setDogOwnrId(ids);
            dogs.setDogPictureAddress(originalFilename);
            test.insertImg(dogs);
            request.getSession().setAttribute("dog_img",originalFilename);
        }
    }

    @RequestMapping(value = "/uploaduser")
    public  void uploaduser(MultipartFile file, HttpServletRequest request,String id) throws IOException{
        // 定义上传的路径
        String path = request.getSession().getServletContext().getRealPath("/user_img/");
        File file1 = new File(path);
        System.out.println(path);
        //判断目录是否存在
        if (!file1.exists()){
            file1.mkdirs();
        }
        if (file!=null){
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            // uuid作为文件名
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            String contentype= file.getContentType();
            String name = contentype.substring(contentype.indexOf("/")+1);
            originalFilename =uuid+"."+name;
            file.transferTo(new File(path+originalFilename));
            Integer ids = Integer.parseInt(id);
            // 图片村道数据库
            Users users = new Users();
            test.updateUserImgById(ids,originalFilename);
            request.getSession().setAttribute("user_img",originalFilename);
            request.setAttribute("info","头像跟新成功，刷新页面可显示");

        }
    }



}
